﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Data;
using System.Globalization;

namespace BadHabit.Factory.Converters {
    
    [ValueConversion(typeof(object), typeof(bool))]
    public class IsReadOnlyTaskConverter : IValueConverter {


        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {

            System.Data.DataRowView dv = value as System.Data.DataRowView;
            if (dv == null)
                return false;

            Data.TaskDataSet.TaskRow task = dv.Row as Data.TaskDataSet.TaskRow;
            if (task == null)
                return false;

            return !CanEditThisTask(task);                    
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return new NotSupportedException();
        }

        /// <summary>
        /// Returns true if the current user can edit other tasks
        /// </summary>
        public  static bool CanEditOtherTasks {
            get {
                if (!Client.User.IsInRole(Factory.TeamRoles.SCRUM_MASTER.ToString()))
                    return false;
                return true;
            }
        }

        public static bool CanEditThisTask(Data.TaskDataSet.TaskRow task) {
            if (task.IsTaskAssigneeNull())
                return true;
            if (!Client.User.Identity.Name.Equals(task.TaskAssignee) && !CanEditOtherTasks)
                return false;

            return true;
        }


    }

}
